#include "RTthreads.h"

#include <string.h>
#include <pthread.h>
#include <math.h>
#include <unistd.h>
#include <fcntl.h>
#include "RTthreads.h"
#include "tools_share.h"
#include "sharemem_base.h"


struct waitsem_thread_params wtsem_slow_follow_task3;
struct waitsem_thread_params wtsem_ctrl4;

int counter1=0;
void CylicThread_Ctrl3(struct waitsem_thread_params *f_p)
{
    counter1++;
    printf("CylicThread_Ctrl_Sem3:%d\n",counter1);
}
int counter4=0;
void CylicThread_Ctrl4(struct waitsem_thread_params *f_p)
{
    counter4++;
    printf("CylicThread_Ctrl_Sem4:%d\n",counter4);
}
int main() 
{
    int i;

    char s[]="/home";
	memcpy(s_PROGRM_MAIN_PATH,s,strlen(s));

    int intkey=get_key(256,s_PROGRM_MAIN_PATH);//256和TestRThreads.cc里面的ctp_fast_task.bindsem_ftokid=256;一致
	
	wtsem_slow_follow_task3.thread_id = 8;
	wtsem_slow_follow_task3.waitsem_task = CylicThread_Ctrl3;
	wtsem_slow_follow_task3.CPU_idx = 2; //
	wtsem_slow_follow_task3.priority=75;
	wtsem_slow_follow_task3.wait_sem_id[0]=build_usr_sem(intkey,3);
	wtsem_slow_follow_task3.wait_sem_id[1]=2;//TestRThreads.cc里面的Semdid2
	build_RTWaitSemTick(&wtsem_slow_follow_task3);


    int intkey2=get_key(257,s_PROGRM_MAIN_PATH);
    wtsem_ctrl4.wait_sem_id[0]=build_usr_sem(intkey2,1);//建立信号量集，包含一个信号量
    wtsem_ctrl4.wait_sem_id[1]=0;//指向第一个信号量。
    wtsem_ctrl4.thread_id = 9;
	wtsem_ctrl4.waitsem_task = CylicThread_Ctrl4;
	wtsem_ctrl4.CPU_idx = 3; //
	wtsem_ctrl4.priority=75;
    build_RTWaitSemTick(&wtsem_ctrl4);//由外部信号量控制的线程

    // while (*(wtsem_slow_follow_task3.p_ctrl)!=-1)
    // {
    //     usleep(1000);
    // }

    sleep(1);

    int cnt=0;
    while (cnt<10)
    {
        usleep(500000);
        cnt++;
        printf("CylicThread_Ctrl_Sem4 will run once: \n");
        unlocksem(wtsem_ctrl4.wait_sem_id[0],wtsem_ctrl4.wait_sem_id[1]);//由外部程序控制CylicThread_Ctrl4的运行
        //unlocksem(wtsem_slow_follow_task3.wait_sem_id[0],wtsem_slow_follow_task3.wait_sem_id[1]);   
    }
     

    return 1;

}
